#!/usr/bin/python
# -*- coding:utf-8 -*-
import xlrd
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
from pyecharts.charts import Page, Pie

#打开文件
data = xlrd.open_workbook(r'网站排名.xlsx')   #打开excel
table = data.sheets()[1] #打开excel的第二个sheet
nrows = table.nrows    #捕获到有效数据的行数

cols = table.col_values(0)[1:]  # 网站名
ym = table.col_values(1)[1:]   # 域名
alexa = table.col_values(2)[1:]  # Alexa周排名
fls = table.col_values(3)[1:]  # 反链数
pm = table.col_values(4)[1:]  # 排名
df = table.col_values(5)[1:]  # 得分

colss = table.col_values(0)[1:20]  # 网站名
flss = table.col_values(3)[1:20]  # 反链数
datas = [list(i) for i in zip(cols,ym,alexa,fls,pm,df)]

# 表格
from pyecharts.components import Table
from pyecharts.options import ComponentTitleOpts
tables = Table()
headers = ['网站名','域名','Alexa周排名','反链数','排名','得分']

tables.add(headers, datas)
tables.set_global_opts(
    title_opts=ComponentTitleOpts(title="网站表格图表")
)
# table.render("网站表格图.html")

# 条形图
bar = (
    Bar(init_opts=opts.InitOpts(theme=ThemeType.LIGHT,bg_color='white'))
        .add_xaxis(cols)
        .add_yaxis("Alexa周排名", alexa)
        .add_yaxis("排名", pm)
        .set_global_opts(title_opts=opts.TitleOpts(title="网站的Alexa周排名对比"),toolbox_opts=opts.ToolboxOpts(is_show=True),datazoom_opts=opts.DataZoomOpts(is_show=True))
)
# bar.render('Alexa周排名条形图.html')

# 饼图
pie=(
    Pie()
        .add(
        "",
        [list(z) for z in zip(colss,flss)],
        radius=["25%", "65%"],
    )
        .set_global_opts(
        title_opts=opts.TitleOpts(title="网站的反链数对比"),
        legend_opts=opts.LegendOpts(
            orient="vertical", pos_top="5%", pos_left="0%"
        ),
    )
        .set_series_opts(label_opts=opts.LabelOpts(formatter="{b}: {c}"))
)
# pie.render('反链数饼图.html')

# 词云图
from pyecharts import options as opts
from pyecharts.charts import Page, WordCloud

cloud = (
    WordCloud()
        .add("", [list(z) for z in zip(cols,df)], word_size_range=[20, 100])
        .set_global_opts(title_opts=opts.TitleOpts(title="网站得分词云图"))
)
# cloud.render('得分词云图.html')

# 网站分析多图组合
page = Page(layout=Page.SimplePageLayout)    # 简单布局
# 将上面定义好的图添加到 page
page.add(
    # tables,
    bar,
    pie,
    cloud,
)
page.render("网站分析多图组合.html")
